package UMC.Data.Sql;

import UMC.Data.Utility;

public class MySqlDbProvider extends DbProvider {
    static {

        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public DbBuilder builder() {
        return new MySqlBuilder();
    }

    class MySqlBuilder extends DbBuilder {
        @Override
        public String AddColumn(String name, String field, String type) {
            return String.format("ALTER TABLE %s ADD %s %s ", name, field, type);
        }

        @Override
        public String Binary() {
            return "blob";
        }

        @Override
        public String Boolean() {
            return "TINYINT";
        }

        @Override
        public String Date() {
            return "DATETIME";
        }

        @Override
        public String DropColumn(String name, String field) {
            return String.format("ALTER TABLE %s DROP %s", name, field);
        }

        @Override
        public String Float() {
            return "FLOAT";
        }

        @Override
        public String Guid() {
            return "CHAR(36)";
        }

        @Override
        public String Integer() {
            return "INTEGER";
        }

        @Override
        public String Number() {
            return "DECIMAL(16,2)";
        }

        @Override
        public String PrimaryKey(String name, String... fields) {
            StringBuilder sb = new StringBuilder();
            sb.append("ALTER TABLE ");
            sb.append(name);
            sb.append(" ADD PRIMARY KEY (");// id);")
            for (int i = 0; i < fields.length; i++) {

                sb.append(fields[i]);//"{0}", s);
                sb.append(',');

            }
            sb.deleteCharAt(sb.length() - 1);
            sb.append(")");
            return sb.toString();
        }

        @Override
        public String String() {
            return "NVARCHAR(500)";
        }

        @Override
        public String Text() {
            return "TEXT";
        }

        @Override
        public Boolean Check(String name, String field, ISqler sqler) {

            int m = Utility.parse(sqler.executeScalar("select count(*) from information_schema.columns where table_name = {0} and column_name = {1}", name, field).toString(), 0);
            return m > 0;
        }

        @Override
        public Boolean Check(String name, ISqler sqler) {
            int m = Utility.parse(sqler.executeScalar("select count(*) from information_schema.tables where table_name = {0}", name).toString(), 0);
            return m > 0;
        }

    }

    @Override
    public String quotePrefix() {
        return "`";
    }

    @Override
    public String quoteSuffix() {
        return "`";
    }

    @Override
    public String paginationText(int start, int limit, String selectText) {
        return String.format("%s limit %d,%d", selectText, start, limit);

    }

    @Override
    public String conntionString() {
        String port = this.provider.get("port");
        if (Utility.isEmpty(port)) {
            port = "3306";
        }
        return String.format("jdbc:mysql://%s:%s/%s", provider.get("host"), port, provider.get("database"));
    }
}
